<?php
/**
 * 在线报名模块
 */
namespace Home\Controller;
class SignupController extends ComController{
    /**
     * 驾校报名
     */
    public function sign(){
        $user = D('user');//实例化会员模型
        $class = D('class_level');//实例化驾校班型模型
        // $uid = session('user_id');
        $uid = 2;
        $d_id = 0;
        if(IS_POST){
            $param = I('post.');//接收参数
            $user = D('user');//实例化会员模型
            $class = D('class_level');//实例化驾校班型模型
            $class_id = $param['class'];
            if(!$class_id){
                $this->error('缺少信息');
            }else{
                $data = $class->where(array('id'=>$class_id))->find();
                if($data){
                    $res = array();//组装数据
                    $res['d_id'] = $data['d_id'];
                    $d_id = $data['d_id'];
                    $res['c_id'] = $data['id'];
                    $res['money'] = $data['price'];
                    $res['uid'] = $uid;
                    $res['user_name'] = $param['user_name'];
                    $res['id_card'] = $param['id_card'];
                    $res['phone'] = $param['phone'];
                    $res['add_time'] = time();

                    //实例化报名记录
                    $sign_record = M('sign_record');
                    $sign_res = $sign_record->data($res)->add();//保存报名记录
                    if($sign_res){
                        //发起支付
                        $param['sign_id'] = $sign_res;
                        $param['uid'] = $uid;
                        $param['d_id'] = $d_id;
                        $this->pay($param);
                    }else{
                        echo json_encode(array('res' =>0,'data'=>'','msg'=>'报名失败,请重试!'));die;
                    }
                }else{
                    $this->error('缺少信息');
                }
            }
        }else{
            //判断用户是否登录
            //获取当前登录用户id
            //获取所报驾校班型id
            $is_login = 1;//登录状态
            if(!$is_login){
                return false;
            }
            $u_id = 2;//获取用户id
            $class_id = I('class_id') ? I('class_id')  : 1; 
            $class_info = $class->where(array('id'=>$class_id))->find();
            if($class_info){
                $res = array();//组装数据
                $res['class_id'] = $class_id;
                $res['driver_type'] = $class_info['type'];
                $where['type'] = $class_info['type']; //驾照类型
                $where['d_id'] = $class_info['d_id'];//驾校id
                //获取同类型下的班型信息 如获取C1下的 VIP 普通班
                $class_data = $class->where($where)->select();

                if($class_data){
                    $res['class_data'] = $class_data;
                }else{
                    $this->error('缺少信息');
                }
                $this->assign('data',$res);
                $this->display();
            }else{
                $this->error('缺少信息');
            }
        }
    }

    /**
     * ajax通过班级id获取 班级信息 
     * http post
     */
    public function getClass(){
        $class_id = I('post.class_id') ? I('post.class_id') : 0; //接收参数
        if($class_id){
            // 实例化驾校班型模型 查询信息
            $class = D('class_level');
            $class_info = $class->where(array('id'=>$class_id))->find();
            if($class_info){
                echo json_encode(array('res'=>1,'data'=>$class_info,'msg'=>'ok'));die;
            }else{
                echo json_encode(array('res'=>0,'data'=>'','msg'=>'没有数据'));die;
            }
        }else{
            echo json_encode(array('res'=>0,'data'=>'','msg'=>'缺少信息'));die;
        }
    }

    /**
     * 短信验证码
     */
    function sendInfo(){
        $phone = I('post.phone') ? I('post.phone') : 0; //接收手机号
        if($phone){

            //验证码生成 6位数字
            $num = '0123456789';//验证码源
            $code = '';
            for ($i=0; $i < 6; $i++) { 
                $code .= rand(0,9);
            }
            // echo $code;die;
            session('sms_code',$code);
            $content = '您的短信验证码为:'.$code;
            $content=iconv('utf-8','gb2312',$content);
            $data=file_get_contents("http://sms.pica.com/zqhdServer/sendSMS.jsp?regcode=ZXHD-CRM-0100-XNHZOT&pwd=7e8b26b7b643d03381b99311ca23fe0b&phone=$mobile&CONTENT=$content&extnum=&level=1&schtime=&reportflag=1&url=&smstype=4&hardKEY=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
            if($data){
                echo json_encode(array('res'=>1,'data'=>'','msg'=>'ok'));
            }else{
                echo json_encode(array('res'=>0,'data'=>'','msg'=>'nofound'));
            }
        }else{
            echo json_encode(array('res'=>0,'data'=>'','msg'=>'nofound'));
        }
    }

    /**
     * 报名缴费
     */
    /*public function pay(){
        // 当前登录用户id
        // $uid = session('user_id'); 
        $uid = 2;
        $param = I('post.');//接收参数
        $user = D('user');//实例化会员模型
        $class = D('class_level');//实例化驾校班型模型
        $class_id = $param['class'];
        if(!$class_id){
            $this->error('缺少信息');
        }else{
            $data = $class->where(array('id'=>$class_id))->find();
            if($data){
                $res = array();//组装数据
                $res['d_id'] = $data['d_id'];
                $res['c_id'] = $data['id'];
                $res['uid'] = $uid;
                $res['user_name'] = $param['user_name'];
                $res['id_card'] = $param['id_card'];
                $res['phone'] = $param['phone'];
                $res['add_time'] = time();

                //实例化报名记录
                $sign_record = M('sign_record');
                $sign_res = $sign_record->data($res)->add();//保存报名记录
                if($sign_res){
                    //发起支付
                    //支付成功更新user表中 报名信息
                    echo '报名成功，支付待对接中原银行接口!';die();
                    if(true){//支付成功

                        $user = M('user');//实例化用户模型
                        $sign_where['d_id'] = $d_id;
                        $sign_where['uid'] = $uid;
                        $sign_where['id'] = $sign_res;
                        $sign_param['is_paid'] = 1;
                        $sign_param['pay_way'] = '中原银行';
                        $sign_param['pay_time'] = time();

                        $user_param['school_id'] = $d_id;//所报教学id
                        $user_param['schedule'] = 1;//科目
                        $user_where['id'] = $uid;
                        
                        $sign_flag = $sign_record->where($sign_where)->save($sign_param);
                        $user_res = $user->where($user_where)->save($user_param);
                        if($sign_flag && $user_res){
                            //信息更新成功
                            echo json_encode(array('res' =>0,'data'=>'','msg'=>'支付成功!'));die;
                        }else{
                            //信息更新失败 自动发起退款 再次请求支付？？？？
                            echo json_encode(array('res' =>0,'data'=>'','msg'=>'支付成功!'));die;
                        }
                    }else{
                        //支付失败
                        echo json_encode(array('res' =>0,'data'=>'','msg'=>'支付失败!'));die;
                    }

                }else{
                    echo json_encode(array('res' =>0,'data'=>'','msg'=>'报名失败,请重试!'));die;
                }
            }else{
                $this->error('缺少信息');
            }
        }
    }*/
    /**
     * [pay 支付成功调用，更新数据]
     * @param  array  $param 报名表中主键sign_id,所报进行d_id,当前用户uid
     * @return [type]        [description]
     */
    public function pay($param = array()){
         //支付成功更新user表中 报名信息
        // echo '报名成功，支付待对接中原银行接口!';die();
        if(true && is_array($param) && isset($param['sign_id']) && isset($param['d_id']) && isset($param['uid'])){//支付成功
            $sign_record = D('sign_record');//实例化报名模型
            $user = M('user');//实例化用户模型
            $sign_where['id'] = $param['sign_id'];
            $sign_param['is_paid'] = 1;
            $sign_param['pay_way'] = '中原银行';
            $sign_param['status'] = 2;//默认为报到
            $sign_param['pay_time'] = time();

            $user_param['school_id'] = $param['d_id'];//所报教学id
            $user_param['schedule'] = 0;//科目
            $user_where['id'] = $param['uid'];
            
            $sign_flag = $sign_record->where($sign_where)->save($sign_param);
            $user_res = $user->where($user_where)->save($user_param);
            if($sign_flag !== false  && $user_res !== false){
                //信息更新成功
                echo json_encode(array('res' =>1,'data'=>'','msg'=>'支付成功!'));die;
            }else{
                //信息更新失败 自动发起退款 再次请求支付？？？？
                echo json_encode(array('res' =>1,'data'=>'错误'.$sign_record->getError().'--'.$user->getError(),'msg'=>'支付成功!'));die;
            }
        }else{
            //支付失败
            echo json_encode(array('res' =>0,'data'=>'','msg'=>'支付失败!'));die;
        }
    }
}
?>